package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service

public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    //新增套餐
    @Override
    public void add(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        //属性拷贝
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表加入1条数据
        setmealMapper.insert(setmeal);
        //获取新增套餐的id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();

        //如果套餐和菜品的关联关系不为空
        if(setmealDishs != null && !setmealDishs.isEmpty()){
            setmealDishs.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });

            //保存套餐和菜品的关联关系
            setmealDishMapper.insertBatch(setmealDishs);

        }
    }
    //更新菜品的销量
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page =  setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //删除套餐
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
//删除套餐并不会影响菜品的完整性。菜品可以独立存在，不依赖于套餐。因此，可以直接删除套餐及其关联关系，而不影响菜品的数据完整性。
        ids.forEach(setmealId ->{
            //删除套餐
            setmealMapper.deleteById(setmealId);
            //删除套餐和菜品的关联关系
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
    }

    //修改套餐
    //1.修改前提根据id查询套餐
    @Override
    public SetmealVO getById(Long id) {

        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //根据套餐id查询套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    //2.实施修改套餐
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //在数据库中修改套餐
        setmealMapper.update(setmeal);
        //删除套餐和菜品的关联关系
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //新增套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            //批量新增套餐和菜品的关联关系
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    //起售停售套餐
    @Override
    public void updateStatus(Integer status, Long id) {

        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(StatusConstant.ENABLE == status){
            //根据套餐id查询菜品
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            //判断菜品是否存在停售的菜品
            if(dishList != null && !dishList.isEmpty()){
                //过滤出停售的菜品
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE.equals(dish.getStatus())){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }

        }
        //修改套餐状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }
    //条件查询套餐
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
    //根据套餐id查询菜品选项
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
